<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\api\controller\v1;

use app\Request;
use app\services\crud\QuestionServices;
use app\services\crud\QuestionCategoryServices;

/**
 * 题目前台控制器
 * Class QuestionController
 * @package app\api\controller\v1
 */
class QuestionController
{
    protected $services;
    protected $categoryServices;

    /**
     * QuestionController constructor.
     * @param QuestionServices $services
     * @param QuestionCategoryServices $categoryServices
     */
    public function __construct(QuestionServices $services, QuestionCategoryServices $categoryServices)
    {
        $this->services = $services;
        $this->categoryServices = $categoryServices;
    }

    /**
     * 获取分类列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function categories()
    {
        $tree = $this->categoryServices->getTreeList();

        return app('json')->success($tree);
    }

    /**
     * 获取题目列表
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        $type = $request->get('type', 0);
        $categoryId = $request->get('category_id', 0);

        if (!$type) {
            return app('json')->fail('请选择题目类型');
        }

        $list = $this->services->getQuestionsByType((int)$type, (int)$categoryId);

        return app('json')->success($list);
    }

    /**
     * 获取题目详情
     * @param Request $request
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(Request $request, $id)
    {
        if (!$id) {
            return app('json')->fail('缺少参数');
        }

        $info = $this->services->getInfo((int)$id);

        // 隐藏敏感数据
        if ($info['type'] == 1) {
            // 解析题隐藏解析内容
            foreach ($info['options'] as &$option) {
                unset($option['analysis']);
            }
        } elseif ($info['type'] == 2) {
            // 分值题隐藏分值
            foreach ($info['options'] as &$option) {
                unset($option['score']);
            }
        }

        return app('json')->success($info);
    }

    /**
     * 获取选项解析（解析题专用，用户答题后调用）
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAnalysis(Request $request)
    {
        $questionId = $request->post('question_id', 0);
        $optionId = $request->post('option_id', 0);

        if (!$questionId || !$optionId) {
            return app('json')->fail('缺少参数');
        }

        $analysis = $this->services->getOptionAnalysis((int)$questionId, (int)$optionId);

        return app('json')->success($analysis);
    }

    /**
     * 提交答题并计算得分（分值题专用）
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function submitAnswers(Request $request)
    {
        $answers = $request->post('answers', []);

        if (empty($answers) || !is_array($answers)) {
            return app('json')->fail('答案数据格式错误');
        }

        // 计算得分
        $result = $this->services->calculateScore($answers);

        return app('json')->success($result);
    }
}

